package Group9.TodoManager.Actions;

import java.awt.event.ActionEvent;

import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import Group9.TodoManager.Settings;
import Group9.TodoManager.Data.SimpleListModel;
import Group9.TodoManager.View.ListUI;

/**
 * RemoveListItemaAction is used to remove tasks from the pop up list.
 * It removes only one task at a time
 * @author volkan cambazoglu
 *
 */
public class RemoveListItemAction extends TodoManagerAction {
	
	private ListUI listUI;
	
	/**
	 * Constructor. Creates an instance of the RemoveListItemAction class.
	 * @param name name of the action
	 * @param icon icon of the action
	 * @param description description of the action
	 * @param ui ListUI that the operation belongs to
	 */
	public RemoveListItemAction(String name, ImageIcon icon, 
				String description, ListUI ui){
		
		super(name, icon);
		putValue(Action.SHORT_DESCRIPTION, description);
		listUI = ui;
	}
	
	/**
	 * When this action is executed, a task will be removed from the list
	 * @param e event
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		SimpleListModel slm = listUI.getListModel();
		int selectedIndex = slm.getSelectedRowIndex();

		if (JOptionPane.showConfirmDialog(Settings.getMainGUI(),
				Settings.getCurrentResourceBundle().getString("Message.DeleteConfirmMsgList"), 
				Settings.getCurrentResourceBundle().getString("Message.DeleteConfirmTitle"),
				JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
			
		    Settings.displayMessage(
		    	Settings.getCurrentResourceBundle().getString("Message.TaskRemovedList"));
		    
		    listUI.removeFromList(slm.getElementAt(selectedIndex));
		}
	}
}
